package com.ge;

import com.ge.config.HeadersCORSFilter;
import com.ge.config.WxMpProperties;
import com.ge.config.WxPayProperties;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.github.tobato.fastdfs.FdfsClientConfig;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;

@Import(FdfsClientConfig.class)
@SpringBootApplication
@EnableConfigurationProperties({WxPayProperties.class,WxMpProperties.class})
//@EnableSwagger2
public class GeApplication extends SpringBootServletInitializer {

	public GeApplication() {
		setRegisterErrorPageFilter(false);
	}

	public static void main(String[] args) {
		SpringApplication.run(GeApplication.class, args);
	}


	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(GeApplication.class);
	}

	@Bean
	public FilterRegistrationBean testFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new HeadersCORSFilter());
		registration.addUrlPatterns("/*");
		registration.setName("HeadersCORSFilter");
		registration.setOrder(1);
		return registration;
	}

	@Autowired
	private WxPayProperties properties;

	@Bean
	public WxPayService wxService() {
		WxPayConfig payConfig = new WxPayConfig();
		payConfig.setAppId("wxeef3ce3e1f64a455");
		payConfig.setMchId("1507521291");
		payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
		payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
		payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
		payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));

		WxPayService wxPayService = new WxPayServiceImpl();
		wxPayService.setConfig(payConfig);
		return wxPayService;
	}

	@Bean
	public WxMpService wxMpService() {
		WxMpInMemoryConfigStorage config  = new WxMpInMemoryConfigStorage ();
		config .setAppId(StringUtils.trimToNull(this.properties.getAppId()));
		config.setSecret("..."); // 设置微信公众号的app corpSecret
		config.setToken("..."); // 设置微信公众号的token
		config.setAesKey("..."); // 设置微信公众号的EncodingAESKey
		WxMpService wxPayService = new WxMpServiceImpl();
		wxPayService.setWxMpConfigStorage(config );
		return wxPayService;
	}

	@Bean
		public EmbeddedServletContainerCustomizer containerCustomizer() {

			return (container -> {
				ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
				ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
				ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

				container.addErrorPages(error401Page, error404Page, error500Page);
			});
	}

//	@Bean
//	public Docket createRestApi() {
//		return new Docket(DocumentationType.SWAGGER_2)
//				.apiInfo(apiInfo())
//				.select()
//				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//				.paths(PathSelectors.any())
//				.build();
//	}
//
//	private ApiInfo apiInfo() {
//		return new ApiInfoBuilder()
//				.title("GE项目RESTful APIs文档")
//				.description("GE项目RESTful APIs文档")
//				.contact("智一优")
//				.version("1.0")
//				.build();
//	}
}
